//
//  PopView.m
//  Animation
//
//  Created by 社区580 on 2018/1/12.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "PopView.h"
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

typedef void(^CoverClickedBlock)(void);

@interface PopView ()
@property (copy, nonatomic) CoverClickedBlock coverClickedBlock;
@end

@implementation PopView

+ (instancetype)popViewShowWithImageArr:(NSArray *)imageArr coverBlock:(void(^)(void))coverBlock {
    PopView *popView = [[PopView alloc] initWitImageArr:imageArr];
    popView.coverClickedBlock = coverBlock;
    [popView showWithCompletion:^{
//        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 / 0.7 options:0 animations:^{
////            popView.center = CGPointMake(KScreenWidth/2, KScreenHeight/2);
//            popView.frame = CGRectMake(KScreenWidth/2 - 100, KScreenHeight/2-150, 200, 300);
//        } completion:nil];
        [UIView animateWithDuration:1.0 animations:^{
            popView.frame = CGRectMake(KScreenWidth/2 - 100, KScreenHeight/2-150, 200, 300);
        }];
    }];
    return popView;
}

- (instancetype)initWitImageArr:(NSArray *)imageArr {
    CGFloat X = - 350;
    CGFloat Y = KScreenHeight - 50;
    CGRect frame = CGRectMake(X, Y, 100, 50);
    if (self = [super initWithFrame:frame]) {
//        self.backgroundColor = [UIColor redColor];
        UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
        view.backgroundColor = [UIColor redColor];
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:view];
        NSArray *hView = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
        NSArray *vView = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
        [self addConstraints:hView];
        [self addConstraints:vView];
    }
    return self;
}

- (void)showWithCompletion:(void(^)(void))completion{
    self.backgroundColor = [UIColor clearColor];
//    [self.layer setBorderWidth:0.5];
//    [self.layer setBackgroundColor:[UIColor lightGrayColor].CGColor];
    if (self.cover) {
        [self.cover removeFromSuperview];
        self.cover = nil;
    }
    
    UIView *window = [UIApplication sharedApplication].keyWindow;
    if (!window) {
        window = [self getCurrentVC].view;
    }
    
    if (!self.cover) {
        _cover = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cover.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.cover.backgroundColor = [UIColor blackColor];
        [self.cover addTarget:self action:@selector(coverClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.cover.alpha = 0.4;
        [window addSubview:self.cover];
    }
    
    self.alpha = 0;
    [window addSubview:self];
    [window bringSubviewToFront:self];
    __weak typeof(self) ws = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        ws.alpha = 1;
    } completion:^(BOOL finished) {
        if (finished) {
            if (completion) {
                completion();
            }
        }
    }];
}

- (void)hide {
    [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping: 0.7 initialSpringVelocity:1/0.7 options:0 animations:^{
        self.frame = CGRectMake(KScreenWidth+100, KScreenHeight - 50, 300, 200);
    } completion:^(BOOL finished) {
        [self.cover removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)coverClicked:(id)sender {
    [self hide];
    if (_coverClickedBlock) {
        _coverClickedBlock();
    }
}

//获取当前显示的视图控制器
- (UIViewController *)getCurrentVC
{
    UIViewController* topVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}
@end
